package com.ccc.boot.generator.controller;

import com.ccc.boot.generator.dto.SettingEntity;
import com.ccc.boot.generator.response.BaseResponse;
import com.ccc.boot.generator.service.MyGeneratorService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;

/**
 * 代码生成 REST接口
 *
 * @author sueprC
 * @date 2022/3/29
 * @version 0.1.0
 */

@Controller
@RequestMapping("/codeGenerator")
public class CodeGeneratorController {

    protected static final Logger logger = LoggerFactory.getLogger(CodeGeneratorController.class);

    @Resource
    private MyGeneratorService myGeneratorService;

    /**
     * 生成代码
     *
     * @date 2022/3/29 17:20
     * @param settingEntity
     * @author superC
     */
    @PostMapping("/generate")
    public BaseResponse generate(@RequestBody SettingEntity settingEntity){
        BaseResponse baseResponse = new BaseResponse();
        long start = System.currentTimeMillis();
        try {
            myGeneratorService.generate(settingEntity);
            baseResponse.setMessage("生成代码成功");
        } catch (Exception e){
            logger.error("【捕获异常】\r\n异常记录：", e);
            baseResponse.setCode(500);
            baseResponse.setMessage("生成代码失败");
        } finally {
            long end = System.currentTimeMillis();
            logger.warn("总计耗时: {}秒", (end-start)/1000);
        }
        return baseResponse;
    }

}
